package com.example.androidtest1;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.util.Log;
import android.view.Menu;

public class ProviderTestActivity extends Activity {
	public static final String TAG = "JINISU";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_provider_test);
		//
		insert();
		query();
		update();
		delete();

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.provider_test, menu);
		return true;
	}

	public static final String AUTHORITY = "com.jinisu.provider.TestProvider";
	public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
			+ "/notes");

	private void insert() {
		ContentValues values = new ContentValues();
		values.put("title", "hello");
		values.put("content", "my name is alex zhou");
		long time = System.currentTimeMillis();
		values.put("create_date", time);
		Uri uri = this.getContentResolver().insert(CONTENT_URI, values);
		Log.e(TAG, uri.toString());
	}

	private void query() {
		Cursor cursor = this.getContentResolver().query(CONTENT_URI, null,
				null, null, null);
		Log.e(TAG, "count=" + cursor.getCount());
		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			String title = cursor.getString(cursor.getColumnIndex("title"));
			String content = cursor.getString(cursor.getColumnIndex("content"));
			long createDate = cursor.getLong(cursor
					.getColumnIndex("create_date"));
			Log.e(TAG, "title: " + title);
			Log.e(TAG, "content: " + content);
			Log.e(TAG, "date: " + createDate);

			cursor.moveToNext();
		}
		cursor.close();
	}

	private void update() {
		ContentValues values = new ContentValues();
		values.put("content", "my name is alex zhou !!!!!!!!!!!!!!!!!!!!!!!!!!");
		int count = this.getContentResolver().update(CONTENT_URI, values,
				"_id=1", null);
		Log.e(TAG, "count=" + count);
		query();
	}

	private void delete() {
		int count = this.getContentResolver()
				.delete(CONTENT_URI, "_id=1", null);
		Log.e(TAG, "count=" + count);
		query();
	}
}
